'use client'

import { addNote, getNote, listNote, updateNote } from "@/clientApi/task/note";
import { globalMessage } from "@/utils/message";
import { ClockCircleOutlined, DoubleLeftOutlined } from "@ant-design/icons";
import { Button, Form, FormProps, Input } from "antd";
import { useEffect, useState } from "react";
import ReminderView from "./reminder";

type FieldType = {
    description?: string;
};
const EditNoteView: React.FC<{ handleCancle: () => void; categoryId: string, noteId: string }> = ({ handleCancle, categoryId, noteId }) => {
    const [form] = Form.useForm();
    useEffect(() => {
        if (noteId) {
            // 编辑逻辑待补充
            loadList();
        }
    }, [noteId]);
    const loadList = () => {
        getNote(noteId).then((res: any) => {
            if (res?.code === 200) {
                form.setFieldsValue({
                    noteId: res.data?.noteId || '',
                    description: res.data?.description || '',
                    reminderId: res.data?.reminderId || '',
                });
            }
        });
    };
    const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
        console.log('Success:', values);
        const data = {
            description: values.description,
            categoryId: categoryId,
        }
        if (noteId !== null && noteId !== undefined && noteId !== '') {
            updateNote({ noteId, ...data }).then((res: any) => {
                console.log("res", res)
                if (res.code == 200) {
                    globalMessage().success(res.msg)
                }
            })
        } else {
            addNote(data).then((res: any) => {
                console.log("res", res)
                if (res.code == 200) {
                    globalMessage().success(res.msg)
                }

            })
        }
        handleCancle();

    };

    const onFinishFailed: FormProps<FieldType>['onFinishFailed'] = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };
    return (

        <div className="bg-[#808080] text-amber-50 p-5 h-screen">
            <div onClick={handleCancle} className="flex items-center select-none">
                <DoubleLeftOutlined /><div className="pl-2">新增便签</div>
            </div>
            <div className="p-4">
                <Form
                    form={form}
                    name="editNote_basic"
                    initialValues={{ remember: true }}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item<FieldType>
                        label="内容"
                        name="description"
                        rules={[{ required: true, message: '在里边记录一条内容吧！~' }]}
                    >
                        <Input.TextArea rows={8} showCount style={{
                            backgroundColor: '#595959',
                            color: 'white'
                        }}
                            className="text-white" />
                    </Form.Item>

                    <div className="flex gap-1 items-center">
                        <ClockCircleOutlined />
                        <div>提醒事项</div>
                    </div>
                    <ReminderView
                        handleSaveReminded={(data: any) => { }}
                        handleCancle={handleCancle}
                        reminderId={form.getFieldValue('reminderId')}
                        noteId={form.getFieldValue('noteId')} />
                    <Form.Item label={null}>
                        <Button type="primary" htmlType="submit">
                            保存
                        </Button>
                    </Form.Item>
                </Form>

            </div>
        </div>



    )

}
export default EditNoteView;